﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace The_Last_Knights.GUI
{
    public class Border: Element2D
    {
        #region Attributes
        protected Texture2D _bc;
        protected Texture2D _lb;
        protected Texture2D _lc;
        protected Texture2D _lt;        
        protected Texture2D _rb;
        protected Texture2D _rc;
        protected Texture2D _rt;        
        protected Texture2D _tc;
#endregion

        #region Properties
        public Texture2D BottomCenter
        {
            get { return _bc; }
            set { _bc = value; }
        }

        public Texture2D LeftBottom
        {
            get { return _lb; }
            set { _lb = value; }
        }

        public Texture2D LeftCenter
        {
            get { return _lc; }
            set { _lc = value; }
        }

        public Texture2D LeftTop
        {
            get { return _lt; }
            set { _lt = value; }
        }

        public Texture2D RightBottom
        {
            get { return _rb; }
            set { _rb = value; }
        }

        public Texture2D RightCenter
        {
            get { return _rc; }
            set { _rc = value; }
        }

        public Texture2D RightTop
        {
            get { return _rt; }
            set { _rt = value; }
        }

        public Texture2D TopCenter
        {
            get { return _tc; }
            set { _tc = value; }
        }
#endregion

        public Border(Game game, string ID, Vector2 pos, Vector2 size, Texture2D background, string borderDir) 
            : base(game, ID, pos, size, background)
        {
            try
            {
                _lt = Game.Content.Load<Texture2D>(GameAssertPath.GUI_TEXTURE_PATH + borderDir + "/lt");    
            }
            catch{}
            
            try
            {
                _tc = Game.Content.Load<Texture2D>(GameAssertPath.GUI_TEXTURE_PATH + borderDir + "/tc");    
            }
            catch{}
            
            try
            {
                _rt = Game.Content.Load<Texture2D>(GameAssertPath.GUI_TEXTURE_PATH + borderDir + "/rt");    
            }
            catch{}

            try
            {
                _lc = Game.Content.Load<Texture2D>(GameAssertPath.GUI_TEXTURE_PATH + borderDir + "/lc");                    
            }
            catch{}

            try
            {
                _rc = Game.Content.Load<Texture2D>(GameAssertPath.GUI_TEXTURE_PATH + borderDir + "/rc");
            }
            catch {}
            
            try
            {
                _rb = Game.Content.Load<Texture2D>(GameAssertPath.GUI_TEXTURE_PATH + borderDir + "/rb");    
            }
            catch {}

            try
            {
                _lb = Game.Content.Load<Texture2D>(GameAssertPath.GUI_TEXTURE_PATH + borderDir + "/lb");    
            }
            catch {}

            try
            {
                _bc = Game.Content.Load<Texture2D>(GameAssertPath.GUI_TEXTURE_PATH + borderDir + "/bc");
            }
            catch{}            
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            SpriteBatch sb = GetSpriteBatch();
            if (sb == null) return;
            sb.Begin();
            try
            {
                if (_lt != null)                    
                    sb.Draw(_lt, new Rectangle((int)_position.X, (int)_position.Y, _lt.Width, _lt.Height), Color.White);                                            

                if (_tc != null &&_lt != null && _rt != null)
                    sb.Draw(_tc, new Rectangle((int)_position.X + _lt.Width, (int)_position.Y,
                                               (int)(_size.X) - _rt.Width - _lt.Width, _tc.Height), Color.White);

                if (_rt != null)
                    sb.Draw(_rt, new Rectangle((int)(_position.X + _size.X) - _rt.Width, (int)_position.Y,
                                               _rt.Width, _rt.Height), Color.White);
                    
                if (_lb != null)
                    sb.Draw(_lb, new Rectangle((int)_position.X, (int)(_position.Y + _size.Y - _lb.Height), _lb.Width, _lb.Height), Color.White);

                if (_lc != null && _lt != null && _lb != null)
                    sb.Draw(_lc, new Rectangle((int)_position.X, (int)(_position.Y + _lt.Height),
                                               _lc.Width, (int)_size.Y - _lb.Height - _lt.Height), Color.White);
                    
                if (_rc != null && _rt != null && _rb != null)
                    sb.Draw(_rc, new Rectangle((int)(_position.X + _size.X) - _rc.Width, (int)(_position.Y + _rt.Height),
                                               _rc.Width, (int)_size.Y - _rb.Height - _rt.Height), Color.White);

                if (_bc != null && _lb != null && _rb != null)
                    sb.Draw(_bc, new Rectangle((int)(_position.X + _lb.Width), (int)(_position.Y + _size.Y - _bc.Height),
                                               (int)(_size.X - _lb.Width - _rb.Width), _bc.Height), Color.White);

                if (_rb != null)
                    sb.Draw(_rb, new Rectangle((int)(_position.X + _size.X - _rb.Width), (int)(_position.Y + _size.Y - _rb.Height),
                                               _rb.Width, _rb.Height), Color.White);
            }
            catch
            {
                    
            }                
            sb.End();
        }
    }
}
